const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");
const Chalk = require('chalk')
const Yargs = require('yargs')
const md5 = require('md5')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

var { argv } = Yargs

let definePlugin = {
	'COPYRIGHT_ENV': JSON.stringify("../../assets/copyright.png"),
	'LOGO_ENV': JSON.stringify("../../assets/logo.png")

}

var webpackConfig = {
	entry: {
		// index: ['./index.tsx'],
		login: ['./src/login.tsx'],
	},
	resolve: {
		extensions: [".ts", ".tsx", ".js"],
		alias: {
			// Base: path.join(__dirname, 'src/Base'),
		}
	},

	output: {
		path: path.join(__dirname, 'build'),
		filename: 'js/[name].js',
		chunkFilename: 'js/[name]-[chunkHash:3].js',
		// publicPath: 'http://sdk-test.changic.net.cn:8191/sdk-demo/'
		// publicPath: '//sdk-android-vndbjh.bilivfun.com/h5sdk/dbjh/'
	},

	module: {
		rules: [
			{
				test: /\.js$/,
				exclude: /node_modules/,
				use: [
					{
						loader: 'babel-loader',
						options: {
							presets: [
								'es2015',
								'react'
							]
						}
					},

				]
			},
			{
				test: /\.css$/,
				use: [
					'style-loader',
					'css-loader'
				]
			},
			{
				test: /\.scss$/,
				use: [
					'style-loader',
					'css-loader',
					'sass-loader'
				]
			},
			{
				test: /\.(ts|tsx)$/,
				use: [
					'ts-loader'
				]
			},
			{
				test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
				use: [
					{
						loader: 'file-loader',
						options: {
							name: '[name]-[hash:4].[ext]',
							outputPath: './img'
						}
					}
				]
			}
		]
	},

	plugins: [
		new HtmlWebpackPlugin({
			filename: "login.html",
			chunks: ['login'],
			template: "./src/template/login.html"
		}),

		new webpack.ProvidePlugin({
			md5: 'md5',
			$: 'jquery'
		}),

		new webpack.DefinePlugin(definePlugin),
		new UglifyJsPlugin({
			sourceMap: false,
			uglifyOptions: {
				mangle: true
			}
		}),
	],

	devServer: {
		contentBase: path.join(__dirname, 'build'),
		inline: true,
		port: 7000,
		// https: true
	}
}

argv.mode === 'production' && webpackConfig.plugins.push(
	new CleanWebpackPlugin(path.join(__dirname, 'build', 'js', '**/*'))
)

module.exports = webpackConfig

